package com.kk.permission.utils.floatwindow;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build.VERSION;
import android.util.Log;

import com.kk.permission.GuideConst;

public class HuaweiUtils {
    private static final String TAG = "HuaweiUtils";

    public static boolean checkFloatWindowPermission(Context context) {
        return VERSION.SDK_INT >= 19 ? checkOp(context, 24) : true;
    }

    @SuppressLint("WrongConstant")
    public static void applyPermission(Context context) {
        Intent intent;
        try {
            Intent intent2 = new Intent();
            intent2.setFlags(268435456);
            intent2.setComponent(new ComponentName("com.huawei.systemmanager", GuideConst.HUAWEI_TOAST_PERMISSION_ACTIVITY_NAME_V2));
            if (RomUtils.getEmuiVersion() == 3.1d) {
                context.startActivity(intent2);
                return;
            }
            intent2.setComponent(new ComponentName("com.huawei.systemmanager", GuideConst.HUAWEI_TOAST_PERMISSION_ACTIVITY_NAME_V3));
            context.startActivity(intent2);
        } catch (Throwable e) {
            intent = new Intent();
            intent.setFlags(268435456);
            intent.setComponent(new ComponentName("com.huawei.systemmanager", GuideConst.HUAWEI_APP_PERMISSION_ACTIVITY_NAME));
            context.startActivity(intent);
            Log.e(TAG, Log.getStackTraceString(e));
        }
    }

    @TargetApi(19)
    private static boolean checkOp(Context context, int i) {
        boolean z = false;
        if (VERSION.SDK_INT >= 19) {
            @SuppressLint("WrongConstant") AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService("appops");
            try {
                if (((Integer) AppOpsManager.class.getDeclaredMethod("checkOp", new Class[]{Integer.TYPE, Integer.TYPE, String.class}).invoke(appOpsManager, new Object[]{Integer.valueOf(i), Integer.valueOf(Binder.getCallingUid()), context.getPackageName()})).intValue() == 0) {
                    z = true;
                }
                return z;
            } catch (Throwable e) {
                Log.e(TAG, Log.getStackTraceString(e));
            }
        } else {
            Log.e(TAG, "Below API 19 cannot invoke!");
            return false;
        }
        return false;
    }
}
